'use strict';
app.controller('agentIntegralCtrl',['$scope','agentIntegralService','$modal', 'utilService', 'toaster',
    function($scope, agentIntegralService, $modal, utilService, toaster){

        $scope.totalIntegral = 0;
        $scope.condition = {};
        $scope.query = function(){
            utilService.queryForPagingList($scope,{
                url:$scope.app.domainAgent + 'manage/integral/getAgentIntegralList',
                params:$scope.condition
            }).then(function(result){});

            agentIntegralService.getTotalIntegral($scope).then(function (result) {
                switch (result.status) {
                    case 200:
                        $scope.totalIntegral = result.data;
                        break;
                    default :
                        toaster.pop('error', "提示", result.message);
                        break;
                }
            });
        }
        $scope.query();

        $scope.agent = {
            integralRecord: {},
            changeType: 0,
            changeTypeName: '增加',
            changeValue: 0,
            description: '',
        }
        // 变更积分窗口
        $scope.openChange = function (record) {
            $scope.agent.integralRecord = record;

            $modal.open({
                templateUrl: 'tpl/agent/agent_integral_change.html',
                controller: 'agentIntegralChangeCtrl',
                scope:$scope,
                resolve: {
                    items: function () {
                        return  $scope;
                    }
                }
            });
        };
    }
]);

app.controller('agentIntegralChangeCtrl', ['$scope', '$modalInstance', 'toaster', '$state', 'agentIntegralService',
    function($scope, $modalInstance, toaster, $state, agentIntegralService) {
        $scope.changeType = function(type) {
            $scope.agent.changeType = type;
            switch (type) {
                case 0:
                    $scope.agent.changeTypeName = '增加';
                    break;
                default:
                    $scope.agent.changeTypeName = '减少';
                    break;
            }
        }

        $scope.isAllowSubmit=true;

        $scope.ok = function(){
            if(!$scope.isAllowSubmit){
                return;
            }
            $scope.isAllowSubmit = false;

            switch($scope.agent.changeType) {
                case 0:
                    $scope.agent.changeValue = Math.abs($scope.agent.changeValue);
                    break;
                default:
                    $scope.agent.changeValue = $scope.agent.changeValue * -1;
                    break;
            }

            agentIntegralService.changeIntegral($scope).then(function (result) {
                    switch (result.status){
                        case 200:
                            $state.reload('app.agentIntegral');
                            $scope.cancel();
                            break;
                        default :
                            toaster.pop('error', "提示", result.message);
                            break;
                    }
                    $scope.isAllowSubmit=true;
            });
        }

        //取消弹窗
        $scope.cancel = function () {
            $scope.agent.changeValue = 0;
            $modalInstance.dismiss('cancel');
        };
    }
]);
